function AfterSearch(data) {
  // data = Process("scripts.utils.AfterSearchForTimeProcess", data);

  for (var i in data.data) {
    data.data[i].status = [data.data[i].status]
    if (data.data[i].os_version) {
      data.data[i].pic = ["/api/__yao/table/cmdb.server/download/fields.table.pic?name=/os/linux.png"];
      let str = data.data[i].os_version;
      if (str.toLowerCase().includes("centos")) {
        data.data[i].os_name = "centos"
        data.data[i].pic = ["/api/__yao/table/cmdb.server/download/fields.table.pic?name=/os/centos.png"];
      } else if (str.toLowerCase().includes("ubuntu")) {
        data.data[i].os_name = "ubuntu"
        data.data[i].pic = ["/api/__yao/table/cmdb.server/download/fields.table.pic?name=/os/ubuntu.png"];
      } else if (str.toLowerCase().includes("redhat")) {
        data.data[i].os_name = "redhat"
        data.data[i].pic = ["/api/__yao/table/cmdb.server/download/fields.table.pic?name=/os/redhat.png"];
      } else if (str.toLowerCase().includes("windows")) {
        data.data[i].os_name = "windows"
        data.data[i].pic = ["/api/__yao/table/cmdb.server/download/fields.table.pic?name=/os/windows.png"];
      } else {
        data.data[i].os_name = "linux"
      }

    } else {
      data.data[i].os_version = "unknow os version"
      data.data[i].os_name = "Unknow"
      data.data[i].pic = ["/api/__yao/table/cmdb.server/download/fields.table.pic?name=/os/linux.png"];
    }
    if (data.data[i].ip && data.data[i].ip.length > 1) {
      data.data[i].ip = data.data[i].ip.join(",");
    }
  }
  console.log(data);
  return data;
}

function Save(payload) {
  console.log("---START-server-save---")
  // console.log(payload);
  // const user_id = Process("session.Get", "user_id");
  // console.log("user_id1",user_id)
  // payload.op_id = user_id;
  console.log("---END-server-save---")
  var res = Process("models.cmdb.server.save", payload);
  return [res];
}


/**
* 在关联处理器 save 之前运行
* @param payload 用户提交数据记录
*/
function BeforeSave(payload) {
  console.log("---BeforeSave---")
  console.log(payload.ip.join("，"))
  let res = Process(
    "models.cmdb.ip.UpdateWhere",
    { "wheres": [{ "column": "name", "value": payload.ip, "op": "in" }] },
    { "status": "assigned" }
  );

  console.log("---END2---");
  console.log(res);
  //const user_id1 = Process("session.Get", "user_id");
  // console.log("user_id1",user_id1)
  //console.log("---END1---")
  return [payload];
}
/**
 * 在关联处理器 save 之后运行
 * @param result 处理器运行结果
 */
function AfterSave(result) {
  console.log("---afterSave---")
  console.log(result)
  return result;
}
function AfterFind(result) {
  //  console.log("---AfterFind-START--")
  // console.log(result)
  // console.log("---AfterFind-END--")
  return result;
}
function Find(id, query, ...args) {
  // console.log("---Find-START--")
  // console.log("args", args)
  let result = Process(
    "models.cmdb.server.Find", id, query
  );
  // console.log("---Find-END--")
  return result;
}
/**
* 在关联处理器 find 之前运行
* @param id 数据记录主键数值
* @param query 查询参数
*/
function BeforeFind(id, query, ...args) {
  ////  console.log("---BeforeFind-START--")
  // console.log(id, query)
  // console.log("args", args)
  // console.log("---BeforeFind-END--")
  return [id, query];
}

/**
 * 在关联处理器 create 之后运行
 * @param result 处理器运行结果
 */
function AfterCreate(result) {
  return result;
}

/**
 * 在关联处理器 update 之后运行
 * @param result 处理器运行结果
 */
function AfterUpdate(result) {
  return result;
}

/**
 * 在关联处理器 delete 之后运行
 * @param result 处理器运行结果
 */
function AfterDelete(result) {
  return result;
}

/**
* 在关联处理器 delete 之前运行
* @param id 数据记录主键数值
* @param query 查询参数
*/
function Delete(id) {
  let server = Process("models.cmdb.server.Find", id, {});
  // console.log("server-2->", server)

  // process check
  let res = Process("models.cmdb.process.Get", { wheres: [{ "column": "server_id", "op": "=", "value": id }] });
  if (res.length > 0) {
    throw new Exception("禁止删除，该服务器上有(" + res.length + ")个进程!", 500);
  }
  // crontab check @TODO
  res = Process("models.cmdb.crontab.Get", { wheres: [{ "column": "server_id", "op": "=", "value": id }] });
  if (res.length > 0) {
    throw new Exception("禁止删除，该服务器上有(" + res.length + ")个crontab job !", 500);
  }
  // platforms check @TODO
  platforms = Process("models.cmdb.platform.Get", { wheres: [{ "column": "deleted_at", "op": "null" }] });
  var serv = [];
  platforms.forEach(element => {
    serv = serv.concat(element.server);
    console.log(serv.length)
  });
  var hasName = serv.some(function (obj) {
    return obj.name === server.name;
  });
  console.log("hasName:", hasName);
  if (hasName) {
    throw new Exception("禁止删除，该服务器上有平台 !", 500);
  }
  // console.log("---------------+++--------------------");

  res = Process("models.cmdb.server.Delete", id, {});
  console.log("res-->", res);
  return [res];
}


